package com.bc004346.travelus;

import android.app.Activity;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.bc004346.travelus.dataAccess.asyncTasks.CompleteBookingTaskFragment;
import com.bc004346.travelus.model.domain.Passenger;
import com.bc004346.travelus.model.domain.Reservation;
import com.bc004346.travelus.model.domain.StringList;
import com.bc004346.travelus.model.interfaces.ICompleteBookingCallback;
import com.bc004346.travelus.utility.Utility;

import java.util.ArrayList;
import java.util.List;

import static com.bc004346.travelus.utility.Utility.COMPLETE_BOOKING_TAG;
import static com.bc004346.travelus.utility.Utility.log;
import static com.bc004346.travelus.utility.Utility.onActivityStart;
import static com.bc004346.travelus.utility.Utility.onActivityStop;

/**
 * Created by Renats on 4/4/2014.
 * T&C static
 */
public class TermsActivity
        extends Activity
        implements ICompleteBookingCallback {

    private FragmentManager mFragmentManager;
    private TextView mMessage;
    private Button mButton;

    @Override
    protected void onStart() {
        super.onStart();
        onActivityStart(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        onActivityStop(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_terms);
        mFragmentManager = getFragmentManager();

        mButton = (Button) findViewById(R.id.activity_terms_button_accept);
        Button decline = (Button) findViewById(R.id.activity_terms_button_decline);
        mMessage = (TextView) findViewById(R.id.activity_terms_textView_message);

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mButton.setText(getString(R.string.common_working));
                CompleteBookingTaskFragment fragment =
                        (CompleteBookingTaskFragment) mFragmentManager.findFragmentByTag(COMPLETE_BOOKING_TAG);
                if (fragment == null) {
                    Passenger pax = Utility.paxSelection.get(Utility.paxSelection.size() - 1);
                    List<String> list = new ArrayList<String>();
                    for (Passenger pass : Utility.paxSelection)
                        list.add(pass.getLogin());
                    fragment = CompleteBookingTaskFragment.newInstance(pax, new StringList(list));
                    mFragmentManager.beginTransaction()
                            .add(fragment, COMPLETE_BOOKING_TAG).commitAllowingStateLoss();
                } else mMessage.setText(getString(R.string.common_please_wait));
            }
        });

        decline.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onCompleteBookingPostExecute(Reservation res, Exception e) {
        mMessage.setText("");
        removeCompleteBookingFragment();

        if (e != null) {
            mMessage.setText(e.getMessage());
            mButton.setText(getString(R.string.common_continue));
            log(e, this);
            return;
        }
        Utility.confirmedReservation = res;
        Intent intent = new Intent(TermsActivity.this, ConfirmationActivity.class);
        startActivity(intent);
        finish();
    }


    private void removeCompleteBookingFragment() {
        CompleteBookingTaskFragment fragment =
                (CompleteBookingTaskFragment) mFragmentManager.findFragmentByTag(COMPLETE_BOOKING_TAG);
        if (fragment != null)
            mFragmentManager.beginTransaction().remove(fragment).commitAllowingStateLoss();
    }
}
